package com.bml.architect.rocketmq.producers;

import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class DefaultProducer {

    @Autowired
    private Source source;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendMsg(String msg) {
        System.out.println("rocketMQTemplate="+rocketMQTemplate);
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(MessageConst.PROPERTY_TAGS, "StreamTag");
        headers.put(MessageConst.PROPERTY_KEYS, "StreamKey");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<String> message = MessageBuilder.createMessage(msg, messageHeaders);
        boolean send = source.output().send(message);
        System.out.println("消息发送结果=" + send);
    }
}
